<?php
class StudentsJobsController extends AppController {

    public $name = 'StudentsJobs';

    /**
     * Open the recruitment road for a student, happens when the first login of students
     * we fake an request for job application, for it only takes 2 key parameters to create and application
     *
     * @param: int $job_id,
     * @param: int $student_id, specified internally, current session student
     */
    public function add($job_id=null){
        if($job_id && !empty($this->user['student'])){
            $this->data['StudentsJob']['job_id'] = $job_id;
            $this->data['StudentsJob']['student_id'] = $this->user['student']['id'];
            if($this->StudentsJob->check($this->data['StudentsJob'])){
                $this->Response->json(false, __('You have applied for the job', true), $this->data);
            }else{
                $this->StudentsJob->create();
                if($this->StudentsJob->save($this->data)){
                    $this->Response->json(true, __('Your job apply request has been saved', true), $this->data);
                }else{
                    $this->Response->json(false, __('Your job apply request could not be saved. Please, try again.', true));
                }
            }
        }else{
            $this->Response->json(false, __('Invalid job apply request', true));
        }
    }

    public function delete($job_id = null){
        if($job_id && !empty($this->user['student'])){
            $this->data['StudentsJob']['job_id'] = $job_id;
            $this->data['StudentsJob']['student_id'] = $this->user['student']['id'];
            $id = $this->StudentsJob->field('id', array(
                'student_id' => $this->user['student']['id'],
                'job_id' => $job_id
            ));
            if(!$this->StudentsJob->check($this->data['StudentsJob'])){
                $this->Response->json(false, __('You have not applied for the job', true), $id);
            }else{
                if($this->StudentsJob->del($id)){
                    $this->Response->json(true, __('Your job apply have been deleted', true), $id);
                }else{
                    $this->Response->json(false, __('Your job apply could not be deleted. Please, try again.', true));
                }
            }
        }else{
            $this->Response->json(false, __('Invalid job apply delete request', true));
        }
    }

}
?>